package com.mall.malladminbackend.controller;

import com.mall.malladminbackend.dto.Create;
import com.mall.malladminbackend.dto.Param;
import com.mall.malladminbackend.entity.SysUser;
import com.mall.malladminbackend.model.R;
import com.mall.malladminbackend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.Date;

/**
 * 作者：郭立仁
 * 描述：永无BUG
 */
@RestController
@RequestMapping("user")
public class UserController { //用户管理
    @Autowired
    private UserService userService;

    @GetMapping("{page}/{pageSize}")
    public R all(@PathVariable Integer page,@PathVariable Integer pageSize){
        return userService.selectAll(page,pageSize,null,null,null);
    }

    @GetMapping
    public R sel(String name, String email, String mobile,Integer pageSize) {
        return userService.selectAll(1,pageSize,name,email,mobile);
    }

    @DeleteMapping("{id}")
    public R del(@PathVariable Integer id){
        return userService.delete(id);
    }

    @GetMapping("{id}")
    public R selectOne(@PathVariable Integer id){
        return userService.selectOne(id);
    }

    @PutMapping("{status}/{id}")
    public R updateStatus(@PathVariable boolean status,@PathVariable Integer id){
        return userService.updateStatus(status,id);
    }

    @PutMapping
    public R updateUser(@RequestBody Param param,Principal principal){
        SysUser sysUser = param.getSysUser();
        int[] roleList = param.getRoleList();
        String name = principal.getName();
        Date date = new Date();
        Create create = new Create(name, date, name, date);
        return userService.update(sysUser,roleList,create);
    }

    @PostMapping
    public R add(@RequestBody Param param,Principal principal){
        SysUser sysUser = param.getSysUser();
        int[] roleList = param.getRoleList();
        String name = principal.getName();
        Date date = new Date();
        Create create = new Create(name, date, name, date);
        return userService.insert(sysUser,roleList,create);
    }
}
